
#include "ControlDef.h"

//已删除全部注释

//VScrollComponent
//////////////////////////////////////////////////////////////////////////
VScrollComponent::VScrollComponent(int Id,UIControl* Owner,int X,int Y)
{
	this->Id=Id;
	this->Owner=Owner;
	this->X=X;
	this->Y=Y;
	this->Width=ScrollWidth;
	this->Height=0;

	HoverHit=FALSE;
	DragPosY=0;
}

VScrollComponent::~VScrollComponent()
{

}

void VScrollComponent::Draw()
{
	D2dMgr->SetColor(0xFFF0F0F0);
	D2dMgr->FillRectangle(0,0,Width,Height);

	if (Pushed)
		D2dMgr->SetColor(0xFF808080);
	else if (Hovered && HoverHit)
		D2dMgr->SetColor(0xFFA0A0A0);
	else
		D2dMgr->SetColor(0xFFC0C0C0);
	D2dMgr->FillRectangle(SliderBorderX,SliderPosY,SliderWidth,SliderHeight);

}

int VScrollComponent::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	int MouseX=(int)Param1;
	int MouseY=(int)Param2;
	if (UIEvent==EvtMouseDown)
	{
		if (PointInRect(MouseX,MouseY,SliderBorderX,SliderPosY,SliderWidth,SliderHeight))
		{

			DragPosY=MouseY-SliderPosY;
		}
		else
		{

			SetSliderPos(MouseY-BodyPosY);
			DragPosY=MouseY-SliderPosY;
		}
	}
	else if (UIEvent==EvtMouseMove)
	{
		HoverHit=FALSE;
		if (PointInRect(MouseX,MouseY,0,SliderPosY,Width-SliderBorderX*2,SliderHeight))
			HoverHit=TRUE;
		if (Pushed)
			SetSliderPos(MouseY-BodyPosY-DragPosY);
	}
	return 0;
}

void VScrollComponent::SetScrollParam(int Height,int PageSize,int TotalSize)
{
	BodyPosY=ArrowSize;

	this->Height=Height;
	this->BodyHeight=Height-ArrowSize*2;

	if (PageSize>=TotalSize)
	{
		this->SliderPosY=0;
		this->Visible=FALSE;
	}
	this->PageSize=PageSize;
	this->TotalSize=TotalSize;

	SliderHeight=BodyHeight*PageSize/TotalSize;
	if (SliderPosY+SliderHeight>BodyHeight)
		SetSliderPos(BodyHeight-SliderHeight);
}

void VScrollComponent::SetSliderPos(int NewSliderPosY)
{
	if (SliderPosY==NewSliderPosY)
		return ;
	SliderPosY=NewSliderPosY;
	if (SliderPosY<BodyPosY)
		SliderPosY=BodyPosY;
	else if (SliderPosY>BodyPosY+BodyHeight-SliderHeight)
		SliderPosY=BodyPosY+BodyHeight-SliderHeight;

	float ScrollPos=SliderPosY*TotalSize/(float)BodyHeight;

	Owner->InternalNotify(Id,EvtScrollTo,(PVOID)Mul1000(ScrollPos),(PVOID)FALSE);
}

void VScrollComponent::SyncScrollData(float ScrollValue)
{
	int NewSliderPosY=BodyHeight*ScrollValue;

	if (SliderPosY==NewSliderPosY)
		return ;

	SliderPosY=NewSliderPosY;
	if (SliderPosY<BodyPosY)
		SliderPosY=BodyPosY;
	else if (SliderPosY>BodyPosY+BodyHeight-SliderHeight)
		SliderPosY=BodyPosY+BodyHeight-SliderHeight;
}

int VScrollComponent::GetBodyHeight()
{
	return BodyHeight;
}

int VScrollComponent::GetSliderPosY()
{
	return SliderPosY;
}



//CVScrollView
//////////////////////////////////////////////////////////////////////////
CVScrollView::CVScrollView(WCHAR* Name,int X,int Y,int Width,int Height)
{
	Type=CT_ScrollView;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Width=Width;
	this->Height=Height;
	this->VirtualHeight=Height;
	this->ViewPosY=0;
	this->Scrollable=TRUE;

	ScrollCpnt=new VScrollComponent(0,this,Width-VScrollComponent::ScrollWidth,0);
	ScrollCpnt->SetScrollParam(Height,Height,VirtualHeight);
	this->ComponentNum=1;
	this->SubCpnt[0]=ScrollCpnt;

	this->ImgBack=NULL;
}

CVScrollView::~CVScrollView()
{
	delete ScrollCpnt;
	if (ImgBack!=NULL)
		ResMgr->ReleaseImageNoType(ImgBack);
	delete[] Name;
}

void CVScrollView::Draw()
{
	if (ImgBack!=NULL)
	{
		D2dMgr->SetImageOpacity(1.0f);
		D2dMgr->DrawImage(0,0,Width,Height,ImgBack);
	}
	if (ScrollCpnt->Visible)
	{
		UiMgr->ShrinkDrawingRect(ScrollCpnt->X,ScrollCpnt->Y,ScrollCpnt->Width,ScrollCpnt->Height);
		ScrollCpnt->Draw();
		UiMgr->RecoverDrawingRect();
	}
}

int CVScrollView::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (!Enable)
		return 0;
	if (UIEvent==EvtMouseWheel)
	{
		SetViewPosY(ViewPosY+(int)Param1*30);
	}
	else if (UIEvent==EvtMouseDown || UIEvent==EvtMouseUp || UIEvent==EvtMouseMove || UIEvent==EvtMouseLeave)
	{
		BOOL CpntHitMiss=(UiMgr->ComponentMouseDispatch(this,UIEvent,Param1,Param2)==-1);
	}
	return 0;
}

int CVScrollView::InternalNotify(int Index,UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (Index==0 && UIEvent==EvtScrollTo && (BOOL)Param2==FALSE)
	{
		SetViewPosY((int)Param1/1000);
	}
	return 0;
}

void CVScrollView::SetBackImage(ImageR_t* ImgBack)
{
	if (this->ImgBack!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgBack);
	this->ImgBack=ImgBack;
}

void CVScrollView::SetVirtualHeight(int VirtualHeight)
{
	ScrollCpnt->Visible=TRUE;
	if (VirtualHeight<=Height)
	{
		VirtualHeight=Height;
		ScrollCpnt->Visible=FALSE;
		ViewPosY=0;
	}
	this->VirtualHeight=VirtualHeight;
	ScrollCpnt->SetScrollParam(Height,Height,VirtualHeight);
}

void CVScrollView::SetHeight(int Height)
{
	ScrollCpnt->Visible=TRUE;
	if (VirtualHeight<=Height)
	{
		VirtualHeight=Height;
		ScrollCpnt->Visible=FALSE;
		ViewPosY=0;
	}
	this->Height=Height;
	ScrollCpnt->SetScrollParam(Height,Height,VirtualHeight);
}

void CVScrollView::SetViewPosY(int ViewPosY)
{
	if (this->ViewPosY==ViewPosY)
		return ;
	if (ViewPosY<0)
		ViewPosY=0;
	if (ViewPosY+Height>VirtualHeight)
		ViewPosY=VirtualHeight-Height;
	this->ViewPosY=ViewPosY;

	ScrollCpnt->SyncScrollData((float)ViewPosY/VirtualHeight);
}

void CVScrollView::SetWidth(int Width)
{
	if (Width<VScrollComponent::ScrollWidth)
		Width=VScrollComponent::ScrollWidth;
	this->Width=Width;
	ScrollCpnt->X=Width-VScrollComponent::ScrollWidth;
}




//CDropList
//////////////////////////////////////////////////////////////////////////
int CDropList::InsertItem(WCHAR* ItemText,int InsertPos)
{
	if (InsertPos<0 || InsertPos>ItemArray.size() || ItemText==NULL)
		return -1;
	if (InsertPos==ItemArray.size())
		return AppendItem(ItemText);

	ItemData* Item=new ItemData;
	Item->Text=wstrdup(ItemText);
	Item->Enable=TRUE;
	Item->UserData=NULL;
	Item->Icon=NULL;
	ItemArray.insert(ItemArray.begin()+InsertPos,Item);
	return InsertPos;
}

int CDropList::AppendItem(WCHAR* ItemText)
{
	if (ItemText!=NULL)
	{
		ItemData* Item=new ItemData;
		Item->Text=wstrdup(ItemText);
		Item->Enable=TRUE;
		Item->UserData=NULL;
		Item->Icon=NULL;
		ItemArray.push_back(Item);
		return ItemArray.size()-1;
	}
	return -1;
}

void CDropList::DeleteItem(int Index)
{
	if (Index<0 || Index>=ItemArray.size())
		return ;

	std::vector<ItemData*>::iterator it=ItemArray.begin()+Index;
	ItemData* Item=*it;
	ItemArray.erase(it);
	if (Item->Icon!=NULL)
		ResMgr->ReleaseImageNoType(Item->Icon);
	delete[] Item->Text;
	delete Item;

	if (Index==SelIndex)
		SetSelect(-1);
}

void CDropList::ClearItem()
{
	std::vector<ItemData*>::iterator it;
	for (it=ItemArray.begin();it!=ItemArray.end();it++)
	{
		ItemData* Item=*it;
		if (Item->Icon!=NULL)
			ResMgr->ReleaseImageNoType(Item->Icon);
		delete[] Item->Text;
		delete Item;
	}
	ItemArray.clear();
	SetSelect(-1);
}

int CDropList::GetItemNum()
{
	return ItemArray.size();
}

WCHAR* CDropList::GetItemText(int Index)
{
	if (Index<0 || Index>=ItemArray.size())
		return NULL;
	ItemData* Item=ItemArray[Index];
	return Item->Text;
}

int CDropList::FindItem(WCHAR* ItemText,int StartIndex)
{
	if (StartIndex<0 || StartIndex>=ItemArray.size() || ItemText==NULL)
		return -1;

	int Index=StartIndex;
	std::vector<ItemData*>::iterator it;
	for (it=ItemArray.begin()+StartIndex;it!=ItemArray.end();it++)
	{
		ItemData* Item=*it;
		if (wcscmp(Item->Text,ItemText)==0)
			return Index;
		Index++;
	}
	return -1;
}

void CDropList::SetItemData(int Index,PVOID Data)
{
	if (Index<0 || Index>=ItemArray.size())
		return ;
	ItemData* Item=ItemArray[Index];
	Item->UserData=Data;
}

PVOID CDropList::GetItemData(int Index)
{
	if (Index<0 || Index>=ItemArray.size())
		return NULL;
	ItemData* Item=ItemArray[Index];
	return Item->UserData;
}

void CDropList::SetItemText(int Index,WCHAR* ItemText)
{
	if (Index<0 || Index>=ItemArray.size())
		return ;
	ItemData* Item=ItemArray[Index];
	delete[] Item->Text;
	Item->Text=wstrdup(ItemText);
}

void CDropList::SetItemIcon(int Index,ImageR_t* Icon)
{
	if (Index<0 || Index>=ItemArray.size())
		return ;
	ItemData* Item=ItemArray[Index];
    if (Item->Icon!=NULL)
		ResMgr->ReleaseImageNoType(Item->Icon);
	Item->Icon=Icon;
}

void CDropList::EnableItem(int Index,BOOL Enable)
{
	if (Index<0 || Index>=ItemArray.size())
		return ;
	ItemData* Item=ItemArray[Index];
	Item->Enable=Enable;
}

CDropList::CDropList(WCHAR* Name,int X,int Y,int Width,BOOL IconStyle)
{
	Type=CT_DropList;
	this->Name=wstrdup(Name);
	this->X=X;
	this->Y=Y;
	this->Scrollable=FALSE;

	this->IconStyle=IconStyle;
	FontText=ResMgr->LoadFontR(L"宋体-14");

	ItemWidth=Width;
	ItemHeight=(IconStyle?IconSize:TextHeight)+Padding1*2;
	BodyWidth=Width;
	BodyHeight=ItemHeight+Padding4*2;
	ListWidth=Width;
	ListHeight=0*ItemHeight;

	SelIndex=-1;
	ExpandState=FALSE;
	DownExpandMode=TRUE;
	ViewIndex=-1;
	ViewItemNum=0;
	HoverIndex=-1;

	this->Width=BodyWidth;
	this->Height=BodyHeight;

	BodyCpnt=new DropListBodyComponent(0,this);
	BodyCpnt->Visible=TRUE;
	BodyCpnt->X=0;
	BodyCpnt->Y=0;
	BodyCpnt->Width=BodyWidth;
	BodyCpnt->Height=BodyHeight;

	ListCpnt=new DropListListComponent(0,this);
	ListCpnt->Visible=FALSE;

	ScrollCpnt=new VScrollComponent(0,this,Width-VScrollComponent::ScrollWidth,0);
	ScrollCpnt->Visible=FALSE;

	this->SubCpnt[0]=BodyCpnt;
	this->SubCpnt[1]=ListCpnt;
	this->SubCpnt[2]=ScrollCpnt;
	this->ComponentNum=3;
}

CDropList::~CDropList()
{
	ClearItem();
	delete ScrollCpnt;
	delete ListCpnt;
	delete BodyCpnt;
	ResMgr->ReleaseFontR(FontText);
	delete[] Name;
}

void CDropList::ExpandList()
{
	if (GetItemNum()==0)
		return ;
	ExpandState=TRUE;

	ScrollCpnt->Visible=FALSE;
	ViewItemNum=GetItemNum();
	if (ViewItemNum>MaxViewItem)
	{
		ScrollCpnt->Visible=TRUE;
		ViewItemNum=MaxViewItem;
	}

	ListHeight=ViewItemNum*ItemHeight;
	ListCpnt->Visible=TRUE;
	HoverIndex=SelIndex;

	DownExpandMode=TRUE;	
	int ScreenX;
	int ScreenY;
	UiMgr->BacktraceScreenPosition(this,&ScreenX,&ScreenY);
	if (ScreenY+BodyHeight+ListHeight>WinViewHeight)
		DownExpandMode=FALSE;
	if (!DownExpandMode)
	{
		OldX=X;
		OldY=Y;
		Y-=ListHeight;
	}

	ViewIndex=SelIndex;
	if (SelIndex==-1 || GetItemNum()<=MaxViewItem)
		ViewIndex=0;
	int AdjustNum=(ViewIndex+ViewItemNum-1)-(GetItemNum()-1);
	if (AdjustNum>0)
		ViewIndex-=AdjustNum;
	
	this->Height=BodyHeight+ListHeight;
	UiMgr->TrySetTopmost(this,TRUE);

	if (DownExpandMode)
	{
		BodyCpnt->X=0;
		BodyCpnt->Y=0;
		BodyCpnt->Width=BodyWidth;
		BodyCpnt->Height=BodyHeight;

		ListCpnt->X=0;
		ListCpnt->Y=BodyHeight;
		ListCpnt->Width=ListWidth;
		ListCpnt->Height=ListHeight;
	}
	else
	{
		ListCpnt->X=0;
		ListCpnt->Y=0;
		ListCpnt->Width=ListWidth;
		ListCpnt->Height=ListHeight;

		BodyCpnt->X=0;
		BodyCpnt->Y=ListHeight;
		BodyCpnt->Width=BodyWidth;
		BodyCpnt->Height=BodyHeight;
	}

	if (ScrollCpnt->Visible)
	{
		ScrollCpnt->SetScrollParam(ListHeight,ViewItemNum,GetItemNum());
		ScrollCpnt->SyncScrollData((float)ViewIndex/GetItemNum());
		ScrollCpnt->X=ScrollCpnt->X;
		if (DownExpandMode)
			ScrollCpnt->Y=BodyHeight;
		else
			ScrollCpnt->Y=0;
	}
    this->Scrollable=TRUE;
}

void CDropList::CollapseList()
{
	BodyCpnt->X=0;
	BodyCpnt->Y=0;
	BodyCpnt->Width=BodyWidth;
	BodyCpnt->Height=BodyHeight;

	ScrollCpnt->Visible=FALSE;
	ListCpnt->Visible=FALSE;
	if (!DownExpandMode)
	{
		X=OldX;
		Y=OldY;
	}
	this->Height=BodyHeight;
	UiMgr->TrySetTopmost(this,FALSE);
    this->Scrollable=FALSE;
	ExpandState=FALSE;
}

void CDropList::SetSelect(int NewIndex)
{
	if (NewIndex>=0 && NewIndex<ItemArray.size())
	{
		if (SelIndex==NewIndex)
			return ;
		SelIndex=NewIndex;

		if (EventHandler!=NULL)
			EventHandler(this,EvtSelectChange,(PVOID)SelIndex,(PVOID)TRUE);
	}
}

int CDropList::GetSelect()
{
	return SelIndex;
}

int CDropList::InternalNotify(int Index,UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (Index==ScrollCpnt->Id && UIEvent==EvtScrollTo && Param2==(BOOL)FALSE)
	{
		int ValueMul1000=(int)Param1;
		int ScrollPosY=ValueMul1000/1000.0f;

		if (ValueMul1000%1000>=500)
			ScrollPosY++;
		ScrollCpnt->SyncScrollData((float)ScrollPosY/GetItemNum());
		ViewIndex=ScrollPosY;
	}
	else if (Index==BodyCpnt->Id && UIEvent==EvtClick)
	{
		if (!ExpandState)
			ExpandList();
		else
			CollapseList();
	}
	else if (Index==ListCpnt->Id && UIEvent==EvtSelectChange)
	{
		CollapseList();
		SetSelect((int)Param1);
	}
	return 0;
}

void CDropList::Draw()
{
	if (BodyCpnt->Visible)
	{
		UiMgr->ShrinkDrawingRect(BodyCpnt->X,BodyCpnt->Y,BodyCpnt->Width,BodyCpnt->Height);
		BodyCpnt->Draw();
		UiMgr->RecoverDrawingRect();
	}

	if (ListCpnt->Visible)
	{
		UiMgr->ShrinkDrawingRect(ListCpnt->X,ListCpnt->Y,ListCpnt->Width,ListCpnt->Height);
		ListCpnt->Draw();
		UiMgr->RecoverDrawingRect();
	}

	if (ScrollCpnt->Visible)
	{
		UiMgr->ShrinkDrawingRect(ScrollCpnt->X,ScrollCpnt->Y,ScrollCpnt->Width,ScrollCpnt->Height);
		ScrollCpnt->Draw();
		UiMgr->RecoverDrawingRect();
	}
}

int CDropList::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (!Enable)
		return 0;
	if (UIEvent==EvtMouseWheel)
	{
		if (ScrollCpnt->Visible)
		{
			int NewViewIndex=ViewIndex+(int)Param1*1;
			int AdjustNum=(NewViewIndex+ViewItemNum-1)-(GetItemNum()-1);
			if (NewViewIndex>=0 && AdjustNum<=0)
			{
				ViewIndex=NewViewIndex;
				ScrollCpnt->SyncScrollData((float)NewViewIndex/GetItemNum());
			}
		}
	}
	else if (UIEvent==EvtBlur)
	{
		if (ExpandState)
			CollapseList();
	}
	else if (UIEvent==EvtMouseDown || UIEvent==EvtMouseUp || UIEvent==EvtMouseMove || UIEvent==EvtMouseLeave)
	{
		BOOL CpntHitMiss=(UiMgr->ComponentMouseDispatch(this,UIEvent,Param1,Param2)==-1);
	}
	return 0;
}


//DropListBodyComponent
//////////////////////////////////////////////////////////////////////////
DropListBodyComponent::DropListBodyComponent(int Id,UIControl* Owner)
{
	this->Id=Id;
	this->Owner=Owner;
	PushedForClick=FALSE;

	ImgNormal=ResMgr->LoadImageR(L"下拉Normal");
	ImgHover=ResMgr->LoadImageR(L"下拉Hover");
	ImgPush=ResMgr->LoadImageR(L"下拉Push");
	ImgDisable=ResMgr->LoadImageR(L"下拉Disable");
}

DropListBodyComponent::~DropListBodyComponent()
{
	if (this->ImgNormal!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgNormal);
	if (this->ImgHover!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgHover);
	if (this->ImgPush!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgPush);
	if (this->ImgDisable!=NULL)
		ResMgr->ReleaseImageNoType(this->ImgDisable);
}

void DropListBodyComponent::Draw()
{
	CDropList* Owner=(CDropList*)this->Owner;
	int IconSize=Owner->IconSize;
	int TextHeight=Owner->TextHeight;
	int MaxViewItem=Owner->MaxViewItem;
	int Padding1=Owner->Padding1;
	int Padding2=Owner->Padding2;
	int Padding4=Owner->Padding4;
	int DropBtnSize=Owner->DropBtnSize;
	int ScrollWidth=Owner->ScrollWidth;

	BOOL IconStyle=Owner->IconStyle;
	int ItemWidth=Owner->ItemWidth;
	int ItemHeight=Owner->ItemHeight;
	int BodyWidth=Owner->BodyWidth;
	int BodyHeight=Owner->BodyHeight;
	int ListWidth=Owner->ListWidth;
	int ListHeight=Owner->ListHeight;

	int SelIndex=Owner->SelIndex;
	BOOL ExpandState=Owner->ExpandState;
	BOOL DownExpandMode=Owner->DownExpandMode;
	int OldX=Owner->OldX;
	int OldY=Owner->OldY;
	int ViewIndex=Owner->ViewIndex;
	int ViewItemNum=Owner->ViewItemNum;
	int HoverIndex=Owner->HoverIndex;

	int NextX=0;
	D2dMgr->SetColor(0xF0FBF2BE);
	D2dMgr->FillRoundRect(NextX,0,BodyWidth,BodyHeight,4);
	NextX+=Padding4;

	if (SelIndex!=-1)
	{
		if (IconStyle)
		{
			ImageR_t* Icon=Owner->ItemArray[SelIndex]->Icon;
			if (Icon!=NULL)
			{
				D2dMgr->SetImageOpacity(1.0f);
				D2dMgr->DrawImage(NextX,Padding4+Padding1,IconSize,IconSize,Icon);
			}
			else
			{
				D2dMgr->SetColor(0x80605040);
				D2dMgr->DrawRectangle(NextX,Padding4+Padding1,IconSize,IconSize);
			}
			NextX+=IconSize+Padding4;
		}
		WCHAR* Text=Owner->ItemArray[SelIndex]->Text;
		if (Text!=NULL)
		{
			int TextX=NextX;
			int TextY=BodyHeight/2-TextHeight/2;
			int TextWidth=BodyWidth-NextX-DropBtnSize-Padding4*2;
			D2dMgr->SetColor(0xFF000000);
			D2dMgr->PushClip(TextX,TextY,TextWidth,TextHeight);
			D2dMgr->DrawText(Text,-1,TextX,TextY,Owner->FontText);
			D2dMgr->PopClip();
		}
	}

	ImageR_t* ImgDraw;
	if (!Enable)
		ImgDraw=ImgDisable;
	else if (Pushed)
		ImgDraw=ImgPush;
	else if (Hovered)
		ImgDraw=ImgHover;
	else
		ImgDraw=ImgNormal;
	D2dMgr->SetImageOpacity(1.0f);
	D2dMgr->DrawImage(BodyWidth-DropBtnSize-Padding4,BodyHeight/2-DropBtnSize/2,DropBtnSize,DropBtnSize,ImgDraw);

	D2dMgr->SetColor(0xFF808080);
	D2dMgr->DrawRoundRect(0,0,BodyWidth,BodyHeight,4);	
}

int DropListBodyComponent::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	if (UIEvent==EvtMouseDown)
	{
		PushedForClick=TRUE;
	}
	else if (UIEvent==EvtMouseUp)
	{
		if (PushedForClick)
		{
			int MouseX=(int)Param1;
			int MouseY=(int)Param2;
			if (PointInRect(MouseX,MouseY,0,0,Width,Height))
				Owner->InternalNotify(Id,EvtClick,0,0);
		}
		PushedForClick=FALSE;
	}
	return 0;
}



//DropListListComponent
//////////////////////////////////////////////////////////////////////////
DropListListComponent::DropListListComponent(int Id,UIControl* Owner)
{
	this->Id=Id;
	this->Owner=Owner;

	PushedForClick=FALSE;
	PushedIndex=-1;
}

DropListListComponent::~DropListListComponent()
{

}

void DropListListComponent::Draw()
{
	CDropList* Owner=(CDropList*)this->Owner;
	int IconSize=Owner->IconSize;
	int TextHeight=Owner->TextHeight;
	int MaxViewItem=Owner->MaxViewItem;
	int Padding1=Owner->Padding1;
	int Padding2=Owner->Padding2;
	int Padding4=Owner->Padding4;
	int DropBtnSize=Owner->DropBtnSize;
	int ScrollWidth=Owner->ScrollWidth;

	BOOL IconStyle=Owner->IconStyle;
	int ItemWidth=Owner->ItemWidth;
	int ItemHeight=Owner->ItemHeight;
	int BodyWidth=Owner->BodyWidth;
	int BodyHeight=Owner->BodyHeight;
	int ListWidth=Owner->ListWidth;
	int ListHeight=Owner->ListHeight;

	int SelIndex=Owner->SelIndex;
	BOOL ExpandState=Owner->ExpandState;
	BOOL DownExpandMode=Owner->DownExpandMode;
	int OldX=Owner->OldX;
	int OldY=Owner->OldY;
	int ViewIndex=Owner->ViewIndex;
	int ViewItemNum=Owner->ViewItemNum;
	int HoverIndex=Owner->HoverIndex;

	for (int i=0;i<ViewItemNum;i++)
	{
		int DrawIndex=ViewIndex+i;

		int NextX=0;
		
		if (DrawIndex==HoverIndex)
			D2dMgr->SetColor(0xC00000FF);
		else
			D2dMgr->SetColor(0xC0000000);

		D2dMgr->FillRectangle(NextX,i*ItemHeight,ItemWidth,ItemHeight);
		NextX+=Padding4;

		if (IconStyle)
		{
			ImageR_t* Icon=Owner->ItemArray[DrawIndex]->Icon;
			if (Icon!=NULL)
			{
				D2dMgr->SetImageOpacity(1.0f);
				D2dMgr->DrawImage(NextX,i*ItemHeight+Padding1,IconSize,IconSize,Icon);
			}
			else
			{
				D2dMgr->SetColor(0x80605040);
				D2dMgr->DrawRectangle(NextX,i*ItemHeight+Padding1,IconSize,IconSize);
			}
			NextX+=IconSize+Padding4;
		}

		WCHAR* Text=Owner->ItemArray[DrawIndex]->Text;
		if (Text!=NULL)
		{
			int TextX=NextX;
			int TextY=ItemHeight/2-TextHeight/2+ItemHeight*i;
			int TextWidth=BodyWidth-NextX-ScrollWidth-Padding4;
			if (Owner->ItemArray[DrawIndex]->Enable)
				D2dMgr->SetColor(0xFFFFFFFF);
			else
				D2dMgr->SetColor(0xFF808080);
			D2dMgr->PushClip(TextX,TextY,TextWidth,TextHeight);
			D2dMgr->DrawText(Text,-1,TextX,TextY,Owner->FontText);
			D2dMgr->PopClip();
		}
	}
}

int DropListListComponent::Notify(UIEvent_t UIEvent,PVOID Param1,PVOID Param2)
{
	CDropList* Owner=(CDropList*)this->Owner;
	int IconSize=Owner->IconSize;
	int TextHeight=Owner->TextHeight;
	int MaxViewItem=Owner->MaxViewItem;
	int Padding1=Owner->Padding1;
	int Padding2=Owner->Padding2;
	int Padding4=Owner->Padding4;
	int DropBtnSize=Owner->DropBtnSize;
	int ScrollWidth=Owner->ScrollWidth;

	BOOL IconStyle=Owner->IconStyle;
	int ItemWidth=Owner->ItemWidth;
	int ItemHeight=Owner->ItemHeight;
	int BodyWidth=Owner->BodyWidth;
	int BodyHeight=Owner->BodyHeight;
	int ListWidth=Owner->ListWidth;
	int ListHeight=Owner->ListHeight;

	int SelIndex=Owner->SelIndex;
	BOOL ExpandState=Owner->ExpandState;
	BOOL DownExpandMode=Owner->DownExpandMode;
	int OldX=Owner->OldX;
	int OldY=Owner->OldY;
	int ViewIndex=Owner->ViewIndex;
	int ViewItemNum=Owner->ViewItemNum;
	int HoverIndex=Owner->HoverIndex;
	
	int MouseX=(int)Param1;
	int MouseY=(int)Param2;
	if (UIEvent==EvtMouseDown)
	{
		PushedForClick=TRUE;
		PushedIndex=MouseY/ItemHeight+ViewIndex;
	}
	else if (UIEvent==EvtMouseUp)
	{
		if (PushedForClick)
		{
			if (PointInRect(MouseX,MouseY,0,0,Width,Height))
			{
				int Index=MouseY/ItemHeight+ViewIndex;
				if (Index==PushedIndex && Owner->ItemArray[Index]->Enable)
					Owner->InternalNotify(Id,EvtSelectChange,(PVOID)Index,(PVOID)TRUE);
			}
		}
		PushedForClick=FALSE;
		PushedIndex=-1;
	}
	else if (UIEvent==EvtMouseMove)
	{
		Owner->HoverIndex=MouseY/ItemHeight+ViewIndex;
	}
	return 0;
}


